namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    /// TODO: implement %i18n attributes
    [XmlRoot("meta")]
    public class Meta : IXmlSerializable
    {
        public Name HttpEquivalent
        {
            get;
            set;
        }

        public Name Name
        {
            get;
            set;
        }

        public string Content
        {
            get;
            set;
        }

        public string Scheme
        {
            get;
            set;
        }

        public static bool operator ==(Meta obj, Meta comparand)
        {
            bool result = false;

            if (object.ReferenceEquals(null, obj))
            {
                result = object.ReferenceEquals(null, comparand);
            }
            else
            {
                result = obj.Equals(comparand);
            }

            return result;
        }

        public static bool operator !=(Meta obj, Meta comparand)
        {
            bool result = false;

            if (object.ReferenceEquals(null, obj))
            {
                result = !object.ReferenceEquals(null, comparand);
            }
            else
            {
                result = !obj.Equals(comparand);
            }

            return result;
        }

        public override bool Equals(object obj)
        {
            bool result = false;

            if (!object.ReferenceEquals(null, obj))
            {
                if (object.ReferenceEquals(this, obj))
                {
                    result = true;
                }
                else
                {
                    Meta cast = obj as Meta;

                    if (!object.ReferenceEquals(null, cast))
                    {
                        result = this.HttpEquivalent == cast.HttpEquivalent
                            && this.Name == cast.Name
                            && this.Content == cast.Content
                            && this.Scheme == cast.Scheme;
                    }
                }
            }

            return result;
        }

        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }

        public XmlSchema GetSchema()
        {
            throw new NotSupportedException();
        }

        public void ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            string value = reader.GetAttribute("http-equiv");
            if (!string.IsNullOrEmpty(value))
            {
                this.HttpEquivalent = value;
            }

            value = reader.GetAttribute("name");
            if (!string.IsNullOrEmpty(value))
            {
                this.Name = value;
            }

            value = reader.GetAttribute("content");
            if (!string.IsNullOrEmpty(value))
            {
                this.Content = value;
            }

            value = reader.GetAttribute("scheme");
            if (!string.IsNullOrEmpty(value))
            {
                this.Scheme = value;
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }

            if (null != this.HttpEquivalent)
            {
                writer.WriteAttributeString("http-equiv", this.HttpEquivalent);
            }

            if (!string.IsNullOrEmpty(this.Name))
            {
                writer.WriteAttributeString("name", this.Name);
            }

            if (!string.IsNullOrEmpty(this.Content))
            {
                writer.WriteAttributeString("content", this.Content);
            }

            if (!string.IsNullOrEmpty(this.Scheme))
            {
                writer.WriteAttributeString("scheme", this.Scheme);
            }
        }
    }
}